package com.hangzhou.xyj.ui.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.GroupModel;
import com.hangzhou.xyj.model.GroupPageInfoModel;
import com.hangzhou.xyj.net.handler.QueryGroupListHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.QueryGroupListReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.MoreGroupAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;

import java.util.List;

/**
 * Created by yaohuix on 2016/4/22  13:50
 * <p/>
 * 说明：搜索页面
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout mRlBack;//返回按钮
    //    private SearchView mSerachView;//搜索框
    private int mPageSize = 15;
    private String mKeyWord;//搜索的文本关键词
    private RecyclerView mRecyclerView;//搜索结果列表
    private MoreGroupAdapter mAdapter;
    private EditText mEtSearchText;//搜索文本
    private View mRlSearchResultView;//搜索结果列表
    private RelativeLayout mRlNoData;//没有数据的时候显示

    @Override
    public void initDatas() {
        mAdapter.setmPageSize(mPageSize);
    }

    @Override
    public void initTop() {
        enableTop(false);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_search);
    }

    @Override
    public void initViews() {
        mRlBack = findView(R.id.rl_back_area);
//        mSerachView = findView(R.id.sv_group);
        mRecyclerView = findView(R.id.rv_common);
        mRlNoData = findView(R.id.rl_no_data);
        mRlSearchResultView = findView(R.id.i_search_result);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        //设置水平分割线
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
//                , R.drawable.line_divider_one));
        mRecyclerView.setAdapter(mAdapter = new MoreGroupAdapter(mRecyclerView, null));
        mEtSearchText = findView(R.id.et_search_text);
    }

    @Override
    public void initListeners() {
        mRlBack.setOnClickListener(this);
//        mSerachView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                showLoadingDialog();
//                mKeyWord = query.trim();
//                getGroupData(1);
//                return true;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String newText) {
//                return false;
//            }
//        });
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int currentPage = mAdapter.getCurrentPage(mPageSize);
                        Log.d(TAG, "page == " + currentPage);
                        getGroupData(currentPage);
                    }
                }, 1000);
            }
        });
        mEtSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//如果是搜索
                    mKeyWord = mEtSearchText.getText().toString().trim();
                    getGroupData(1);
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back_area:
                this.finish();
                break;
        }
    }


    public void getGroupData(final int currentPage) {
        QueryGroupListReq queryGroupListReq = new QueryGroupListReq(mKeyWord, mPageSize, currentPage);
        HttpLoader.getInstance().sendRequest(new QueryGroupListHandler(queryGroupListReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<GroupModel> groupList = ((GroupPageInfoModel) model).getGroupList();
                hideLoadingDialog();
                if (currentPage == 1 && groupList.size() == 0) {
                    mRlSearchResultView.setVisibility(View.GONE);
                    mRlNoData.setVisibility(View.VISIBLE);
                    return;
                } else if (currentPage == 1) {
                    mAdapter.load(groupList);
                } else {
                    mAdapter.update(groupList);
                }
                mRlSearchResultView.setVisibility(View.VISIBLE);
                mRlNoData.setVisibility(View.GONE);
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }
}
